﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;

namespace AMSService
{
    [DataContract (IsReference=true)]
    public class DBConnector
    {
        protected DataTable SendReadRequest(string StoredProc, List<SqlParameter> Params)
        {
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(Utility.GetSqlConnString());
                conn.Open();
                SqlCommand cmd = CreateCommand(StoredProc, Params, conn);
                SqlDataReader sqlr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                DataTable dt = new DataTable();
                dt.Load(sqlr);
                conn.Close();
                return dt;
            }
            catch (SqlException sqlex)
            {
                Console.WriteLine("SqlException at DBConnector::SendWriteRequest -> " + sqlex.Message);
                return null;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception at DBConnector::SendWriteRequest -> " + ex.Message);
                return null;
            }
        }

        protected DataTable SendReadRequest(string StoredProc, SqlParameter[] Params)
        {
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(Utility.GetSqlConnString());
                conn.Open();
                SqlCommand cmd = CreateCommand(StoredProc, Params, conn);
                SqlDataReader sqlr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                DataTable dt = new DataTable();
                dt.Load(sqlr);
                conn.Close();
                return dt;
            }
            catch (SqlException sqlex)
            {
                Console.WriteLine("SqlException at DBConnector::SendWriteRequest -> " + sqlex.Message);
                return null;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception at DBConnector::SendWriteRequest -> " + ex.Message);
                return null;
            }
        }

        protected int SendWriteRequest(string StoredProc, List<SqlParameter> Params)
        {
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(Utility.GetSqlConnString());
                conn.Open();

                SqlCommand cmd = CreateCommand(StoredProc, Params, conn);
                SqlParameter retval = new SqlParameter("@return", SqlDbType.Int);
                retval.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(retval);
                cmd.ExecuteNonQuery();
                conn.Close();
                return (int)cmd.Parameters["@return"].Value;
            }
            catch (SqlException sqlex)
            {
                Console.WriteLine("SqlException at DBConnector::SendWriteRequest -> " + sqlex.Message);
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception at DBConnector::SendWriteRequest -> " + ex.Message);
                return 0;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }

        private SqlCommand CreateCommand(string _spName, List<SqlParameter> _spParams, SqlConnection conn)
        {
            //setup a SQL command to run the stored procedure
            SqlCommand cmd = new SqlCommand(_spName, conn);
            cmd.CommandType = CommandType.StoredProcedure;

            //add all SQL Parameters to the command
            foreach (SqlParameter param in _spParams)
            {
                cmd.Parameters.Add(param);
            }

            return cmd;
        }

        private SqlCommand CreateCommand(string _spName, SqlParameter[] _spParams, SqlConnection conn)
        {
            // Set up a SQL command to run the stored procedure
            SqlCommand cmd = new SqlCommand(_spName, conn);
            cmd.CommandType = CommandType.StoredProcedure;

            // Add all SQL parameters to the command
            for (int i = 0; i < _spParams.Length; i++)
            {
                cmd.Parameters.Add(_spParams[i]);
            }

            return cmd;
        }
    }
}